package com.sort;

public class sort {
    public static void main(String[] args) {
        int[] ints={1,2,3,5,2,9,4,7,6,2,8,3,4};
        sort3(ints);
        for (int i:ints){
            System.out.print(i+" ");
        }
    }
    //冒泡排序
    public static void sort(int[] a) {
        //外层循环控制比较的次数
        for (int i = 0; i < a.length - 1; i++) {
            int n=0;
            //内层循环控制到达位置
            for (int j = 0; j < a.length - i - 1; j++) {
                //前面的元素比后面大就交换
                if (a[j] > a[j + 1]) {
                    n=1;
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
            if (n==0){
                break;
            }
        }
    }
    //选择排序
    public static void sort2(int[] a){
        int n=0;
        int temp;
        for (int i = 0; i < a.length-1; i++) {
            n=0;
            for (int j = 0; j < a.length-i; j++) {
                if (a[j]>a[n]){
                    n=j;
                }
            }
            temp=a[n];
            a[n]=a[a.length-1-i];
            a[a.length-1-i]=temp;
        }

    }
    public static void sort3(int[] a){
        for (int i = 0; i < a.length; i++) {
            int num=a[i];
            int j;
            for ( j = i; j >0&&a[j-1]>num ; j--) {
                a[j]=a[j-1];
            }
            a[j]=num;

        }
    }

}
